package service;


import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.ehcache.CacheKit;
import com.jfinal.plugin.ehcache.IDataLoader;

import config.model.Announcement;
import utils.FileUtils;

public class AnnouncementService {

	/**
	 * 此方法用于添加公告
	 * @param ann 添加的公告信息
	 * 
	 */
	
	public void addAnnouncement(Announcement ann) {
		CacheKit.removeAll("announcement");
		ann.save();
	}
	
	/**
	 * 此方法用于删除公告
	 * @param id 需要删除的公告id
	 * 
	 */
	
	public void deleteAnnouncement(String id) {
		String file = Db.findById("announcement", id).getStr("file").toString();
		FileUtils f = new FileUtils();
		f.DeleteFolder(System.getProperty("user.dir")+"/WebRoot/file/announcement/"+file);
		Db.deleteById("announcement", id);
		CacheKit.removeAll("announcement");
	}
	
	/**
	 * 此方法用于分页查询公告
	 * @param pageNumber 需要显示的公告页码
	 * @return Announcement类型，为需要的公告页码中所有的公告信息
	 * 
	 */
	
	public Page<Announcement> queryAnnouncementPage(int pageNumber) {
		Page<Announcement> announcement = CacheKit.get("announcement", "announcement"+pageNumber, new IDataLoader(){
		       public Object load() {
		           return Db.paginate(pageNumber, 5, "select *","from announcement");
		    }});
		return announcement;
	}
}
